package com.neuedu.jdboot.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neuedu.jdboot.bean.Privilege;
import com.neuedu.jdboot.common.JdbootException;
import com.neuedu.jdboot.mapper.PrivilegerMapper;
import com.neuedu.jdboot.service.PrivilegeService;

/**
 * 
 * @author chens
 *
 */

@Service
@Transactional(rollbackFor = JdbootException.class)
public class PrivilegeServiceImpl extends DefaultServiceImpl<Privilege> implements PrivilegeService {

	@Autowired
	private PrivilegerMapper privilegerMapper;

	@Override
	public List<Privilege> findTreePrivileges() {
		List<Privilege> ret = new ArrayList<>();

		try {
			// 一级菜单 - 操作目录
			ret = privilegerMapper.findByParentId(null);
			for (Privilege privilege : ret) {
				// 二级菜单 - 操作目录
				List<Privilege> subList = privilegerMapper.findByParentId(privilege.getId());
				privilege.setSubList(subList);
				// 三级菜单 - 操作按钮
				for (Privilege thirdPrivilege : subList) {
					thirdPrivilege.setSubList(privilegerMapper.findByParentId(thirdPrivilege.getId()));
				}
			}
		} catch (Exception e) {
			throw new JdbootException("查询异常", e);
		}
		return ret;
	}

}
